﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using Application.MainModule.SoundexCodesManagement;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;

namespace DistributedServices.MainModule
{
	public partial class MainModuleService
	{
        public void AddSoundexCode(SoundexCode soundexCode)
        {
            try
            {
                ISoundexCodeManagementService soundexCodeService = IoCFactory.Instance.CurrentContainer.Resolve<ISoundexCodeManagementService>();
                soundexCodeService.AddSoundexCode(soundexCode);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void ChangeSoundexCode(SoundexCode soundexCode)
        {
            try
            {
                ISoundexCodeManagementService soundexCodeService = IoCFactory.Instance.CurrentContainer.Resolve<ISoundexCodeManagementService>();
                soundexCodeService.ChangeSoundexCode(soundexCode);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveSoundexCode(SoundexCode soundexCode)
        {
            try
            {
                ISoundexCodeManagementService soundexCodeService = IoCFactory.Instance.CurrentContainer.Resolve<ISoundexCodeManagementService>();
                soundexCodeService.RemoveSoundexCode(soundexCode);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public List<SoundexCode> FindSoundexCodeByUserId(Guid userId)
        {
            try
            {
                ISoundexCodeManagementService soundexCodeService = IoCFactory.Instance.CurrentContainer.Resolve<ISoundexCodeManagementService>();

                return soundexCodeService.FindSoundexCodeByUserId(userId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public List<SoundexCode> FindSoundexCodeByValue(string value)
        {
            try
            {
                ISoundexCodeManagementService soundexCodeService = IoCFactory.Instance.CurrentContainer.Resolve<ISoundexCodeManagementService>();

                return soundexCodeService.FindSoundexCodeByValue(value);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }
	}
}
